package cn.itcast.user.web;


import cn.itcast.user.config.PatternProperties;
import cn.itcast.user.pojo.User;
import cn.itcast.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@RequestMapping("/user")
//@RefreshScope
public class UserController {

    @Autowired
    private UserService userService;

 /*   @Value("${pattern.dateformat}")
    private String dateformat;*/

    @Autowired
    private PatternProperties patternProperties;



    @Value("${server.port}")
    int port;
    /**
     * 路径： /user/110
     *
     * @param id 用户id
     * @return 用户
     */
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id,@RequestHeader(value = "name", required = false) String name) {
        log.warn("请求头name的值：{}",name );
        User user = userService.queryById(id);
        user.setId(Long.valueOf(port));
        return user;
    }

    @PostMapping("/save")
    public User saveUser(@RequestBody User user) {
        System.out.println("保存user信息");
        return user;
    }

    @GetMapping("now")
    public String now(){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(patternProperties.getDateformat()));
    }

    @GetMapping("prop")
    public PatternProperties prop(){
        return patternProperties;
    }
}
